#include "Enemy.h"
#include "XMLHelper.h"
#include "Physics.h"


Enemy::Enemy()
{
	mp_animations = new Animation[10];
	m_currentFrame = 0;
	SetState(STATE_WALKLEFT);
	LoadAnimations();
	GetCollider()->SetType(Collider::ENEMY);
}
Enemy::Enemy(int x0, int y0, int w, int h, cBicho::Type type) : cBicho(x0,y0,w,h, type)
{
	mp_animations = new Animation[10];
	m_currentFrame = 0;
	SetState(STATE_WALKLEFT);
	LoadAnimations();
	GetCollider()->SetType(Collider::ENEMY);
}


Enemy::~Enemy()
{
	delete[] mp_animations;
}

void Enemy::Logic(Tile* map)
{
	int x, y;
	GetPosition(&x, &y);
	switch (GetState())
	{
		case STATE_WALKLEFT:
			x -= 3;
			SetPosition(x, y);
			if (CollidesMapWall(map, false))
			{
				SetState(STATE_WALKRIGHT);
			}
			break;
		case STATE_WALKRIGHT:
			x += 3;
			SetPosition(x, y);
			if (CollidesMapWall(map, true))
			{
				SetState(STATE_WALKLEFT);
			}
			break;
	}
	Collider * collidingObject = Physics::IsCollidingDynamic(GetCollider(), Physics::All);
	if (collidingObject && collidingObject->GetType()==Collider::FRIENDLYPROJECTILE)
	{
		SetState(STATE_READYFORDELETION);
	}
	if (!CollidesMapFloor(map))
	{
		y -= (2 * STEP_LENGTH);
		SetPosition(x, y);
	}
}

void Enemy::Draw(int tex_id)
{
	float xo, yo, xf, yf;
	switch (GetState())
	{
		case STATE_WALKLEFT:
			xo = mp_animations[0].GetFrame(m_currentFrame).first.first / 325.0;
			yo = mp_animations[0].GetFrame(m_currentFrame).first.second / 106.0;
			xf = mp_animations[0].GetFrame(m_currentFrame).second.first / 325.0;
			yf = mp_animations[0].GetFrame(m_currentFrame).second.second / 106.0;
			m_currentFrame = NextFrame(7);
			break;
			//4..6
		case STATE_WALKRIGHT:
			xo = mp_animations[1].GetFrame(m_currentFrame).first.first / 325.0;
			yo = mp_animations[1].GetFrame(m_currentFrame).first.second / 106.0;
			xf = mp_animations[1].GetFrame(m_currentFrame).second.first / 325.0;
			yf = mp_animations[1].GetFrame(m_currentFrame).second.second / 106.0;
			m_currentFrame = NextFrame(7);
			break;
		default:
			return;
	}
	DrawRect(tex_id, xo, yo, xf, yf);
}

void Enemy::LoadAnimations()
{
	Animation animation;
	int x0, y0, xf, yf;
	XMLHelper helper;
	helper.Load("Assets\\animations.xml");
	char * frame;
	frame = (char *)malloc(sizeof(char)* 30);
	strcpy(frame, "animation/walkingLeftEnemy/frame/");
	for (int i = 0; i < 7; ++i)
	{
		char * newFrame = (char *)malloc(sizeof(char)* 30);
		strcpy(newFrame, frame);
		char * frameNumber;
		frameNumber = (char *)malloc(sizeof(char)* 2);
		frameNumber[0] = '1' + i;
		frameNumber[1] = '\0';
		strcat(newFrame, frameNumber);
		x0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionX0").c_str());
		y0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionY0").c_str());
		xf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionXF").c_str());
		yf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionYF").c_str());
		mp_animations[0].AddFrame(x0, y0, xf, yf);
		free(frameNumber);
	}

	strcpy(frame, "animation/walkingRightEnemy/frame/");
	for (int i = 0; i < 7; ++i)
	{
		char * newFrame = (char *)malloc(sizeof(char)* 30);
		strcpy(newFrame, frame);
		char * frameNumber;
		frameNumber = (char *)malloc(sizeof(char)* 2);
		frameNumber[0] = '1' + i;
		frameNumber[1] = '\0';
		strcat(newFrame, frameNumber);
		x0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionX0").c_str());
		y0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionY0").c_str());
		xf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionXF").c_str());
		yf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionYF").c_str());
		mp_animations[1].AddFrame(x0, y0, xf, yf);
		free(frameNumber);
	}
}
